package org.jeecg.modules.demo.com.kunpeng.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.demo.com.kunpeng.entity.OaProject;
import org.jeecg.modules.demo.com.kunpeng.entity.dto.OaProjectQueryDTO;
import org.jeecg.modules.demo.com.kunpeng.service.ICommodityPriceService;
import org.jeecg.modules.demo.com.kunpeng.service.IOaContractService;
import org.jeecg.modules.demo.com.kunpeng.service.IOaProjectService;
import org.jeecg.modules.demo.com.kunpeng.service.IProjectPurchaseService;
import org.jeecg.modules.demo.com.kunpeng.vo.CollectionRecordVO;
import org.jeecg.modules.demo.com.kunpeng.vo.OaProjectPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Api(tags="OA项目表")
@RestController
@RequestMapping("/com.kunpeng/oaProject")
@Slf4j
public class OaProjectController {

    @Autowired
    private IOaProjectService oaProjectService;

    @Autowired
    private IProjectPurchaseService projectPurchaseService;

    @Autowired
    private ICommodityPriceService commodityPriceService;

    @Autowired
    private IOaContractService oaContractService;

    /**
     * OA项目表-根据项目名称获取项目详情
     * @param name
     * @return
     */
    @ApiOperation(value = "OA项目表-根据项目名称获取项目详情",notes = "OA项目表-根据项目名称获取项目详情")
    @GetMapping(value = "/selectOaProjectByName")
    public Result<OaProject> selectOaProjectByName(String name){
        OaProject oaProject = oaProjectService.selectOaProjectByName(name);
        return Result.ok(oaProject);
    }

    /**
     * OA项目表-获取项目名称列表
     */
    @ApiOperation(value = "OA项目表-获取项目名称列表",notes = "OA项目表-获取项目名称列表")
    @GetMapping(value = "/listProjectName")
    public Result<List<String>> listProjectName(){
        List<String> projectNameList = oaProjectService.listProjectName();
        return Result.ok(projectNameList);
    }

    /**
     * 分页查询OA项目表
     * @param oaProjectQueryDTO
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "OA项目表-根据项目名分页列表查询", notes = "OA项目表-根据关键字分页列表查询")
    @PostMapping(value = "/listByKeyWords")
    public Result<Void> listByKeyWords(@RequestBody OaProjectQueryDTO oaProjectQueryDTO,
                                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                   @RequestParam(name = "pageSize", defaultValue = "1000") Integer pageSize) {
        log.info("开始处理根据关键字查询项目列表请求...");
        Result<Page<OaProject>> result = new Result<Page<OaProject>>();
        Page<OaProject> pageList = new Page<OaProject>(pageNo, pageSize);
        pageList = oaProjectService.projectList(pageList, oaProjectQueryDTO);//通知公告消息
        if (pageList.getRecords().size() == 0) {
            return Result.error("没有符合条件的数据，请重新输入！");
        }
        /*log.info("查询当前页：" + pageList.getCurrent());
        log.info("查询当前页数量：" + pageList.getSize());
        log.info("查询结果数量：" + pageList.getRecords().size());
        log.info("数据总数：" + pageList.getTotal());*/
        result.setSuccess(true);
        result.setResult(pageList);
        return Result.ok();
    }


    /**
     * 插入oa项目编号
     * @param oaProjectPage
     * @return
     */
    @ApiOperation(value = "OA项目表-新增OA项目数据", notes = "OA项目表-新增OA项目数据")
    @PostMapping(value = "/insertOaProject")
    public Result<String> insertOaProject(@RequestBody OaProjectPage oaProjectPage){
        System.out.println("oaProjectPage:"+oaProjectPage.toString());
        return Result.ok("插入成功！");
    }

    /**
     * 项目编号生产器
     */
    @ApiOperation("OA项目表-项目编号或采购单号生成器")
    @GetMapping(value = "/createOaProjectIdOrPurchaseId")
    public Result<String> createProjectOrPurchaseId(String prefix){
        LocalDate now = LocalDate.now();
        DateTimeFormatter dtf= DateTimeFormatter.ofPattern("yyyy-MM-dd"); //定义时间格式
        String dateStr = now.format(dtf); //转换成String类型
        String[] split = dateStr.split("-");
        if(prefix.equals("XM")) {
            String projectId = "XM" + split[0]+split[1] + split[2];
            System.out.println("项目前缀："+projectId);
            Integer projectIdSuffix = oaProjectService.listOaProjectIdByPrefix(projectId);
            if(projectIdSuffix<=9){
                projectId+="00"+projectIdSuffix;
            }else if(projectIdSuffix>9 && projectIdSuffix<=99){
                projectId+="0"+projectIdSuffix;
            }else if(projectIdSuffix>99){
                projectId+=projectIdSuffix;
            }
            return Result.OK(projectId);
        }else if(prefix.equals("CGSQ")){
            String purchaseId = "CGSQ" + split[0]+split[1] + split[2];
            Integer purchaseIdSuffix = projectPurchaseService.listPurchaseIdByPurchaseId(purchaseId);
            if(purchaseIdSuffix<=9){
                purchaseId+="00"+purchaseIdSuffix;
            }else if(purchaseIdSuffix>9 && purchaseIdSuffix<=99){
                purchaseId+="0"+purchaseIdSuffix;
            }else if(purchaseIdSuffix>99){
                purchaseId+=purchaseIdSuffix;
            }
            return Result.OK(purchaseId);
        }else if(prefix.equals("SP")){
            String commodityCode="SP"+split[0]+split[1] + split[2];
            Integer commodityCodeSuffix = commodityPriceService.commodityPriceListByCode(commodityCode);
            if(commodityCodeSuffix<=9){
                commodityCode+="00"+commodityCodeSuffix;
            }else if(commodityCodeSuffix>9 && commodityCodeSuffix<=99){
                commodityCode+="0"+commodityCodeSuffix;
            }else if(commodityCodeSuffix>99){
                commodityCode+=commodityCodeSuffix;
            }
            return Result.OK(commodityCode);
        }else if(prefix.equals("HT")){
            String contractCode="HT"+split[0]+split[1] + split[2];
            Integer oaContractCodeSuffix = oaContractService.oaContractListByContractIdPrefix(contractCode);
            if(oaContractCodeSuffix<=9){
                contractCode+="00"+oaContractCodeSuffix;
            }else if(oaContractCodeSuffix>9 && oaContractCodeSuffix<=99){
                contractCode+="0"+oaContractCodeSuffix;
            }else if(oaContractCodeSuffix>99){
                contractCode+=oaContractCodeSuffix;
            }
            return Result.OK(contractCode);
        }
        return Result.error("项目编号或采购单号生成失败!");
    }

    /**
     * 根据id修改OA项目的已回款金额值
     */
    @ApiOperation("OA项目表-根据id修改OA项目的已回款金额值")
    @PostMapping(value = "/updateOaProjectAlreadyReturnMoney")
    public Result<String> updateOaProjectAlreadyReturnMoney(@RequestBody CollectionRecordVO collectionRecordVO){
        System.out.println("开始处理根据id修改OA项目回款金额");
        oaProjectService.updateOaProjectAlreadyReturnMoney(collectionRecordVO);
        Page<OaProject> pageList = new Page<OaProject>(1, 1000);
        OaProjectQueryDTO oaProjectQueryDTO=new OaProjectQueryDTO();
        oaProjectService.projectList(pageList,oaProjectQueryDTO);
        return Result.ok("修改成功");
    }

    /**
     * 根据关联客户获取项目列表
     */
    @ApiOperation("OA项目表-根据关联客户id获取项目列表")
    @GetMapping(value = "/oaProjectListByCustomerId")
    public Result<List<OaProject>> oaProjectListByCustomerId(Integer customerId){
        List<OaProject> oaProjects = oaProjectService.oaProjectListByCustomerId(customerId);
        return Result.ok(oaProjects);
    }
}
